//Copyright 2007 Iain Bapty
//
//This file is part of the ITunes Tag Manager.
//
//ITunes Tag Manager is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//ITunes Tag Manager is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with ITunes Tag Manager.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using iTunesLib;

namespace ITunesManager
{
    public partial class Form1 : Form
    {
        private Manager manager;

        public Form1()
        {
            InitializeComponent();
            manager = new Manager();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                listBox1.Items.Clear();
                List<IITTrack> tracks = manager.GetArtists_Capitalised();
                label1.Text = tracks.Count.ToString();
                foreach (IITTrack track in tracks)
                {
                    listBox1.Items.Add(track);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void PauseButton_Click(object sender, EventArgs e)
        {
            manager.Pause();
        }

        private void PlayButton_Click(object sender, EventArgs e)
        {
            manager.Play();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            IITTrack track = (IITTrack) listBox1.SelectedItem;
            label2.Text = textBox1.Text = StringHelper.fixStringCapatalisation(track.Artist);
            button2.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            IITTrack track = (IITTrack)listBox1.SelectedItem;
            track.Artist = StringHelper.fixStringCapatalisation(track.Artist);
            listBox1.SelectedItem = track;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void buttonRemoveMissing_Click(object sender, EventArgs e)
        {
            try
            {

                this.Cursor = Cursors.WaitCursor;
                manager.DeleteMissingFiles();
                LabelDeletedFiles.Text = manager.MissingFilesDeleted.ToString();
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void tabMissingFiles_Click(object sender, EventArgs e)
        {

        }

        private void buttonAddMissingFiles_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK != folderBrowserDialog1.ShowDialog())
            {
                return;
            }
            try
            { 
                this.Cursor = Cursors.WaitCursor;
                String selectedPath = folderBrowserDialog1.SelectedPath;
                manager.FindFilesNotInLibrary(selectedPath);
                labelFilesAdded.Text = "Files Added: " + manager.AddedFilesToLibraryOK;
                labelFilesNotAdded.Text = "Files Not Added: " + manager.AddedFilesToLibraryNotOk;
            }
            finally
            { 
                this.Cursor = Cursors.Default; 
            }
        }
    }
}